window.onload = function () {
    var btn = document.getElementById("all");
    btn.onclick = function () {
        var flag = this.checked;
        var items = document.getElementsByName("sub");
        for (var i = 0; i < items.length; i++) {
            items[i].checked = flag;//将所有item的状态设为全选按钮的状态
        }
    }

    var items = document.getElementsByName("sub");
    for (var i = 0; i < items.length; i++) {
        items[i].onclick = function () {//对每个item设置点击
            var number = 0;//记录选中的个数
            for (var j = 0; j < items.length; j++) {
                if (items[j].checked) {
                    number++;
                }
            }
            document.getElementById("all").checked = (items.length == number);
        }
    }
}

var currPage = 1
// 每页默认5条数据
var pageSize = 5
// 获取传递的参数  /showhero.html?currPage=2--?currPage=2
var search = location.search
// 按照=对参数分割
var arr = search.split("=")
// 第一次访问页面时，是没有传递参数的，只有在分页时才会传递参数
// 防止第一次出现问题

if (arr.length > 1) {
    currPage = arr[1].split("&")[0]
}

if (currPage > 0 && currPage < 100) {
    page = currPage;
} else {
    page = 1;
}


// $.ajax({
//     url: "http://localhost:8080/depot/depotall",
//     data: "currPage=" + page + "&pageSize=" + pageSize,
//     type: "get",
//     dataType: "json",
//     success: function (data) {
//         //console.log(data)
//         let html = ''
//         $.each(data.data.list, function (index, value) {
//             html += `<tr>
//              <td class="umino-product-remove" style="vertical-align: middle">
//                 <input type="checkbox" name="sub">&nbsp;&nbsp;
//                 <button type="button" data-id="${value.depotId}" value="${value.depotId}" data-toggle="modal" data-target="#myModal">
//                   <i class="fa fa-trash" title="Remove" ></i>
//                 </button>
//               </td>
//
//               <td class="umino-product-thumbnail" style="vertical-align: middle">
//                    <a href="javascript:void(0)">${value.depotGoodId}</a>
//                    <a href=""></a>
//               </td>
//
//               <td class="product-subtotal" style="vertical-align: middle">
//               <span class="amount">${value.depotGoodSize}</span>
//               </td>
//
//               <td class="quantity">
//                  <div class="cart-plus-minus">
//                     <input class="cart-plus-minus-box" value="${value.depotGoodCount}" type="number" onblur="updateDepot(this)">
//                  </div>
//               </td>
//               </tr>`
//         })
//         $("table").append(html)
//         generatePage(data.data.navigatepageNums, data.data.pageNum
//             , data.data.pages)
//     }
// })


function selByTime() {
    //var conTime = $("#selTime").val()
    console.log($("#selTime").val())
    $.ajax({
        url: "http://localhost:8080/depot/depotByTime",
        data: "currPage=" + page + "&pageSize=" + pageSize + "&conTime=" + $("#selTime").val(),
        type: "get",
        dataType: "json",
        success: function (data) {
            //console.log(data)
            let html = ''
            $.each(data.data.list, function (index, value) {
                html += `<tr>
             <td class="umino-product-remove" style="vertical-align: middle">
                <input type="checkbox" name="sub">&nbsp;&nbsp;
                <button type="button" data-id="${value.depotId}" value="${value.depotId}" data-toggle="modal" data-target="#myModal">
                  <i class="fa fa-trash" title="Remove" ></i>
                </button>
              </td>
              
              <td class="umino-product-thumbnail" style="vertical-align: middle">
                   <a href="javascript:void(0)">${value.depotGoodId}</a>
                   <a href=""></a>
              </td>
              
              <td class="product-subtotal" style="vertical-align: middle">
              <span class="amount">${value.depotGoodSize}</span>
              </td>   
                                              
              <td class="quantity">
                 <div class="cart-plus-minus">
                    <input class="cart-plus-minus-box" value="${value.depotGoodCount}" type="number" onblur="updateDepot(this)">
                 </div>
              </td>
              </tr>`
            })
            $("table").append(html)
            generatePage(data.data.navigatepageNums, data.data.pageNum
                , data.data.pages)
        }
    })
}

function updateDepot(obj) {
    var num = $(obj).val()
    var id = $(obj).parent().parent().prev().prev().prev().children().next().val()
    //console.log(id + num)
    $.ajax({
        url: "http://localhost:8080/depot/updDepots",
        data: "info=" + id + "," + num,
        type: "get",
        dataType: "json",
        success: function (data) {
            if (data.data == 1) {
                //location.reload();
            } else {
                alert("库存修改失败")
            }
        }
    })
}

// 当前页
function generatePage(navigatepageNums, pageNum, pages) {
    var html = ''
    if (pageNum == 1) {
        html += `<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
    } else {
        html += `<li><a href="http://localhost:63343/qiguai/cart/depot.html?currPage=${pageNum - 1}&pageSize=5&conTime=$('#selTime').val()" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
    }
    // console.log(navigatePages)
    // 生成页码
    for (let i = 0; i < navigatepageNums.length; i++) {
        if (pageNum == navigatepageNums[i]) {
            // 当前页class=active，设置选中效果
            html += `<li class="active"><a href="#">${navigatepageNums[i]}</a></li>`
        } else {
            // showhero.html?
            // 不是当前页
            html += `<li><a href="http://localhost:63343/qiguai/cart/depot.html?currPage=${navigatepageNums[i]}&pageSize=5&conTime=$('#selTime').val()">${navigatepageNums[i]}</a></li>`
        }

    }

    if (pageNum == pages) {
        html += `<li class="disabled"><a href="#" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a></li>`
    } else {
        html += `<li><a href="http://localhost:63343/qiguai/cart/depot.html?currPage=${pageNum + 1}&pageSize=5&conTime=$('#selTime').val()" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a></li>`
    }
    $("#ulNav").append(html)
}

var depId;

$('#myModal').on('show.bs.modal', function (event) {
    // 触发模态框的按钮
    var button = $(event.relatedTarget) // Button that triggered the modal

    // 从data-*内容中获取数据
    depId = button.data('id') // Extract info from data-* attributes
    // 获取到modal 模态框对象
    var modal = $(this)
    modal.find('.modal-title').text('库存删除')

})

$("#myModal .modal-footer button").eq(1).click(function () {
    $.getJSON("http://localhost:8080/depot/delOneDep?depotId=" + depId, data => {
        if (data == 1) {
            location.reload();
        } else {
            alert("删除失败")
        }
    })
    $('#myModal').modal('toggle')
})

var delids

function fre() {

    delids = $("input:checkbox[name='sub']:checked").map(function (index, elem) {
        return $(elem).next().val()
    }).get().join(',');

}

function delChos() {
    fre()
    //alert(delids)
    $.ajax({
        url: "http://localhost:8080/depot/delDepots",
        type: "get",
        data: "ids=" + delids,
        dataType: "json",
        success: function (data) {
            if (data.data > 0) {
                alert("库存删除成功")
                location.reload();
            } else {
                alert("库存删除失败")
            }
        }
    })
}

function upload() {
    $("#files").click();
}


function tipFun(event){
    $('#myModa2').on('show.bs.modal', function (event) {
        // 触发模态框的按钮
        var modal = $(this)
        modal.find('.modal-title').text('插入成功')

    })
}

$(document).ready(function() {

    $("#form1").ajaxForm(function(data){
        // $("#btntip").click()
        if(data.code == 1){
            // $('#myModa2').on('show.bs.modal', function (event) {
            //     // 触发模态框的按钮
            //     var modal = $(this)
            //     modal.find('.modal-title').text('插入成功')
            //
            // })
            alert("插入成功")
            location.reload()
        }
        else {
            // $('#myModa2').on('show.bs.modal', function (event) {
            //     // 触发模态框的按钮
            //     var modal = $(this)
            //     modal.find('.modal-title').text('插入失败')
            //
            // })
            alert("插入失败")
        }
    });
});

function subscribeBtn() {
    alert("订阅成功！")
}
